package thread;

public class Stack {
	private char[] arr = new char[5];
	private int cursor=0;
	
	public synchronized void push(char c){//锁是Stack的锁
		while(cursor==5){
			try {
				//执行到wait()方法的线程等待
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		arr[cursor]=c;
		cursor++;
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("pro");
		notifyAll();
	}
	
	public synchronized char pop(){//锁是Stack的锁
		while(cursor==0){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		cursor--;
		char c = arr[cursor];
		arr[cursor]=0;
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("con");
		notifyAll();
		return c;
	}
}
